package com.blockmeta.bbs.businesslibrary.widget.chartview.Layer;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;


import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.util.Format2Util;
import com.blockmeta.bbs.businesslibrary.websocket.Symbol;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.Coordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.MoveCoordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.ChartData;

import java.util.HashMap;
import java.util.List;

/**
 * kbar和ma的合并
 */
class KBarMoveLayer extends BasicLayer
{
	protected final double m_dbNone = Double.MIN_VALUE;
	List<CandleData> tMAList;


	public KBarMoveLayer()
	{
		super();
	}

	@Override
	public Layer.ChartTypeEnum getLayerTypeEnum()
	{
		return Layer.ChartTypeEnum.KMaMoveBar;
	}

	protected int getDataStartIndex()
	{
		return 0;
	}

	/**
	 * 获取数据的移动位置
	 *
	 * @return
	 */
	protected int getMoveStartIndex()
	{
		Coordinate tCoordinate = m_callback.getCoordinate();
		if (tCoordinate != null)
		{
			return tCoordinate.getStartIndex();
		}
		return 0;
	}

	@Override
	public Coordinate createCoordinate()
	{
		return new MoveCoordinate();
	}

	@Override
	public boolean onCalculateData()
	{
		return true;
	}


	@Override
	public void onDrawLayer(Canvas pCanvas, Paint pPaint, Point pPoint)
	{
		drawKbar(pCanvas, pPaint, pPoint);
	}


	protected void drawKbar(Canvas pCanvas, Paint pPaint, Point pPoint)
	{
//		FDTLogUtils.d("tingchart","KBarMoveLayer ondraw");
		List<CandleData> tDataList = m_callback.getCandleDataList();
		Coordinate tCoordinate = m_callback.getCoordinate();
		int digit = m_callback.getDigit();
		Symbol symbol = m_callback.getCoordinate().tSymBol;
		int volumnDigit = 2;
		if(symbol != null) {
			volumnDigit = symbol.getVolumnDigit();
		}
		float scaleFactor = tCoordinate.getScaleFactor();

		float tBeforeCloseFloat = 0;
		float tCloseFloat = 0;
		float tOpenFloat = 0;
		float tHighFloat = 0;
		float tLowFloat = 0;
		float tValueFloat = 0;
		float tVolumnLong = 0;
		float tAvgFloat = 0;

		CandleData tTimeCandle = null;

		int tXPoint = pPoint.x;
		if (tDataList != null && tDataList.size() > 0)
		{
			int tListSize = tDataList.size();
			int tStartIndex = tCoordinate.getStartIndex();//获取绘制数据集中index
			float tXFirstPosition = tCoordinate.getXFirstPosition();//获取第一个点要画的位置m_rectfLayerRect.left + m_fXInterval
			float tXInterval = tCoordinate.getXInterval();//获取两个x数据点之间的距离，是动态的
//			float tYFirstposition = tCoordinate.getLayerRectF().bottom;
			float tYFirstposition = tCoordinate.getYFirstPosition();
			float tYMaxValue = tCoordinate.getMaxValue();//获取要绘制的数据集中的最大值
			float tYMinValue = tCoordinate.getMinValue();//获取要绘制的数据集中的最小值
			float tYValueDist = tYMaxValue - tYMinValue;//获取数据y上的变化范围
			float tYValueLength = tCoordinate.getValueHeight();//获取rect的高度
			// float tYValueLength = tCoordinate.getYInterval() *
			// tCoordinate.getYIntervalCount();

			float tBarWidth = Math.min(tXInterval / 3, 25);//每个bar的宽度设为之间距离的三分之一，距离根据缩放后的距离，不大于5

			float tXPosition = 0.f;
			float tTempXPosition = 0.f;
			float tCloseYPosition = 0.f;
			float tTempYPosition = 0.f;

			int tSysUpColor = m_callback.getSysUpColor();
			int tSysDownColor = m_callback.getSysDownColor();
			int tSysEvenColor = m_callback.getSysEvenColor();

			boolean tStartDraw = false;

			pPaint.setAntiAlias(true);
			pPaint.setStrokeWidth(2);

			int displayCount = m_callback.getDisplayCount();//获取当前要显示的个数
			int minCount = Math.min(tStartIndex + displayCount, tListSize);//不能超出数据集宽度

			if (m_callback.isDrawAllData())//如果需要绘制全部数据的
			{
				tStartIndex = 0;
				minCount = tListSize;
			}


			drawAvgLine(pCanvas, pPaint, tCoordinate, tYFirstposition, tYMinValue, tYMaxValue, tYValueDist, tYValueLength);

//			FDTLogUtils.d("tingchart","KBarMoveLayer from draw tStartIndex"+tStartIndex+" to mincount"+minCount);
			for (int i = tStartIndex; i < minCount; i++)
			{
				CandleData tCandleData;

				try
				{
					tCandleData = tDataList.get(i);
				}
				catch (Exception e)
				{
					continue;
				}

				if (tCandleData == null)
				{
					continue;
				}

				float tHigh = (float) tCandleData.getDbHigh();//获取当前点的数据
				float tLow = (float) tCandleData.getDbLow();
				float tOpen = (float) tCandleData.getDbOpen();
				float tClose = (float) tCandleData.getDbClose();
				float tAvg = (float) tCandleData.m_dbAvgPx;
				float tVolumn = tCandleData.m_lVolume;


				float tHighYPosition = tYFirstposition - ((tHigh - tYMinValue) * tYValueLength / tYValueDist);//获取high的y位置
				float tLowYPosition = tYFirstposition - ((tLow - tYMinValue) * tYValueLength / tYValueDist);//获取low的y位置
				float tOpenYPosition = tYFirstposition - ((tOpen - tYMinValue) * tYValueLength / tYValueDist);//获取open的y位置

				tTempYPosition = tCloseYPosition;
				tCloseYPosition = tYFirstposition - ((tClose - tYMinValue) * tYValueLength / tYValueDist);//获取close的y位置

				tTempXPosition = tXPosition;
				tXPosition = tXFirstPosition + tXInterval * (i - tStartIndex);//获取x坐标上的值

				float tRectTopPosition = 0.f;
				float tRectBottomPosition = 0.f;

//				drawMaxMin(pCanvas, pPaint, tCoordinate, digit, tXPosition, i, tCandleData);
				drawLineMaxMin(pCanvas, pPaint, tCoordinate, digit, tXPosition, i, tCandleData,tHighYPosition,tLowYPosition);



				if (((tXPosition - tXInterval / 2) <= pPoint.x && pPoint.x < (tXPosition + tXInterval / 2)) || ((i == minCount - 1) && pPoint.x > tXPosition + tXInterval / 2) || (i == tStartIndex && pPoint.x < tXPosition - tXInterval / 2))
				{
					float tValueaa = (tYFirstposition - pPoint.y) * tYValueDist / tYValueLength + tYMinValue;
					tValueFloat = tValueaa;
					ChartData.ChartPeroid tChartType = m_callback.getChartType();
					tVolumnLong = tVolumn;
					tCloseFloat = tClose;
					tOpenFloat = tOpen;
					tHighFloat = tHigh;
					tLowFloat = tLow;
					tTimeCandle = tDataList.get(i);
					tAvgFloat = tAvg;

					tXPoint = (int) tXPosition;

					try
					{
						if (i > 0)
						{
							float tBeforeClose = (float) tDataList.get(i - 1).getDbClose();
							tBeforeCloseFloat = tBeforeClose;
						}
						else
						{
							tBeforeCloseFloat = 0;
						}
					}
					catch (Exception e)
					{
						e.printStackTrace();
					}
				}

				if (tOpen > tClose || (tOpen == tClose && i != 0 && tClose < (float) tDataList.get(i - 1).getDbClose()))
				{
					pPaint.setColor(tSysDownColor);
					pPaint.setStyle(Paint.Style.FILL_AND_STROKE);
					tRectTopPosition = tOpenYPosition;
					tRectBottomPosition = tCloseYPosition;
				}
//				else if (tOpen == tClose)
//				{
//					pPaint.setColor(tSysEvenColor);
//					pPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//					tRectTopPosition = tCloseYPosition;
//					tRectBottomPosition = tOpenYPosition;
//				}
				else
				{
					pPaint.setColor(tSysUpColor);
//					pPaint.setStyle(Paint.Style.STROKE);
					pPaint.setStyle(Paint.Style.FILL_AND_STROKE);
					tRectTopPosition = tCloseYPosition;
					tRectBottomPosition = tOpenYPosition;
				}

				if (scaleFactor <= 0.25)//如果放到一定程度，k线变成线
				{
					if (tStartDraw)
					{
						pPaint.setColor(0xFF4A86EA);
						pCanvas.drawLine(tTempXPosition, tTempYPosition, tXPosition, tCloseYPosition, pPaint);
					}
					else
					{
						tStartDraw = true;
					}
				}
				else
				{
					//画bar
					if (tRectTopPosition == tRectBottomPosition)//如果收盘等于开盘价画线
					{
						pCanvas.drawLine(tXPosition - tBarWidth, tRectTopPosition, tXPosition + tBarWidth, tRectBottomPosition, pPaint);
					}
					else
					{

						pCanvas.drawRect(tXPosition - tBarWidth, tRectTopPosition, tXPosition + tBarWidth, tRectBottomPosition, pPaint);
					}
//				FDTLogUtils.d("tingchart","draw chart i"+i+" tRectTopPosition"+tRectTopPosition+" "+tRectBottomPosition);
					//画线，如果最高最低价格在开盘收盘价格内部，就不画
					pCanvas.drawLine(tXPosition, tRectTopPosition, tXPosition, tHighYPosition, pPaint);
					pCanvas.drawLine(tXPosition, tRectBottomPosition, tXPosition, tLowYPosition, pPaint);
				}

			}
		}


		RectF tLayerRect = tCoordinate.getLayerRectF();


		String time = tTimeCandle == null ? "" : getDisplayX(m_callback.getChartType(), tTimeCandle);

		setCrossData(pCanvas, pPaint, time,
				FormatUtil.getFormatString(tValueFloat, m_callback.getDigit()),
				new Point(tXPoint, pPoint.y), tTimeCandle);

		if(tTimeCandle != null) {
//			LogUtils.d("chartline", "kbarmalayer time:" + time + " int time" + tTimeCandle.getLog());
		}
		if (tLayerRect.contains(tXPoint, pPoint.y))
		{
			HashMap<String, String> tMap = new HashMap<String, String>();
			tMap.put("Close", tCloseFloat == 0 ? "" : FormatUtil.getFormatString(tCloseFloat, digit));
			tMap.put("Open", tOpenFloat == 0 ? "" : FormatUtil.getFormatString(tOpenFloat, digit));
			tMap.put("High", tHighFloat == 0 ? "" : FormatUtil.getFormatString(tHighFloat, digit));
			tMap.put("Low", tLowFloat == 0 ? "" : FormatUtil.getFormatString(tLowFloat, digit));
			tMap.put("BeforeClose", tBeforeCloseFloat == 0 ? "" : FormatUtil.getFormatString(tBeforeCloseFloat, digit));
			tMap.put("Volumn", tVolumnLong == 0 ? "" : Format2Util.doubleToHumanReadableFormat(tVolumnLong, volumnDigit));
			tMap.put("Time", time);
			tMap.put("Avg", tAvgFloat == 0 ? "" : FormatUtil.getFormatString(tAvgFloat, digit));
			m_callback.pointFeedback(tMap,pPoint );
		}
	}


	/**
	 * draw layer left－top text
	 * 获取指标值进行显示
	 *
	 * @param pCanvas
	 * @param pPaint
	 */
	@Override
	public void drawFloatIndicatorText(boolean longtouch, Canvas pCanvas, Paint pPaint)
	{

	}



	public void drawQ(Canvas canvas, Paint paint, Point dest)
	{

//		FDTLogUtils.d("chen","drawQ,point:"+dest.toString());
		paint.setStyle(Paint.Style.FILL);
		paint.setStrokeWidth(2);
		paint.setColor(Color.parseColor("#CE3F2C"));
		paint.setTextSize(DimentionsUtil.sp2px(14));
		canvas.drawText("q", dest.x - paint.getTextSize() / 3.5f, dest.y + paint.getTextSize() / 3, paint);

	}

	@Override
	public boolean isDrawAvgText()
	{
		return true;
	}

}
